$(function () {
    /*上下钟管理*/
    //用localStorage存储状态
    var l_free=localStorage.arrFree,
        l_working=localStorage.arrWorking,
        l_rest=localStorage.arrRest;
    console.log("free:"+l_free);
    console.log("working:"+l_working);
    console.log("rest:"+l_rest);
    function ul_append(localSt_free,localSt_working,localSt_rest){
        if(localSt_free){
            var resArr1 = localSt_free.split(',');
            var ul_free = $('.js-sortable-free');
            //li 数组
            for(var i = 0;i < resArr1.length;i++){
                $("#" + resArr1[i]).find("[value='free']").attr("selected","selected").end()
                    .closest("li").removeClass("m-element-rest m-element-working").addClass("m-element-free")
                    .appendTo(ul_free);
            }
        }
        if(localSt_working){
            var resArr2 = localSt_working.split(',');
            var ul_working = $('.js-sortable-working');
            //li 数组
            for(var i = 0;i < resArr2.length;i++){
                $("#" + resArr2[i]).find("[value='working']").attr("selected","selected").end()
                    .closest("li").removeClass("m-element-rest m-element-free").addClass("m-element-working")
                    .appendTo(ul_working);
            }
        }
        if(localSt_rest){
            var resArr3 = localSt_rest.split(',');
            var ul_rest = $('.js-sortable-rest');
            //li 数组
            for(var i = 0;i < resArr3.length;i++){
                $("#" + resArr3[i]).find("[value='rest']").attr("selected","selected").end()
                    .closest("li").removeClass("m-element-working m-element-free").addClass("m-element-rest")
                    .appendTo(ul_rest);
            }
        }
    }
    ul_append(l_free,l_working,l_rest);

    /*todo 实现空闲中的拖拽功能*/
    var el = document.getElementById('js-ul-free');
    // new Sortable(el);
    var sortable = new Sortable(el,{
        animation: 100,
        // delay: 500,
        handle:'.m-element-free',
        onStart:function (evt) {
            var itemEl=evt.item;
            // alert("111");
        },
        onEnd:function (evt) {
            var itemEl=evt.item;
            getWaiterStates();
        }
    });

    /*

    //记录拖拽后的排序
    $(".js-sortable-free").sortable({
        connectWith: ".js-sortable-free",
        update:function(){
            //记录sort后的id顺序数组
            var arrFree = $( ".js-sortable-free" ).sortable('toArray');
            console.log(arrFree);
            //拖拽后利用localStorage记录顺序
            localStorage.arrFree = arrFree;
        }
    }).disableSelection();
    //如果有localst记录则，按照这个进行排序元素
    */
    /*获取技师三种状态的列表*/
    function getWaiterStates(){
        var i,n,m;
        var li_free_len=$(".js-sortable-free li").length;
        var li_working_len=$(".js-sortable-working li").length;
        var li_rest_len=$(".js-sortable-rest li").length;
        var arrFree=[],
            arrWorking=[],
            arrRest=[];
        for(i=0;i<li_free_len;i++){
            var id_free=$(".js-sortable-free li").eq(i).attr("id");

            arrFree.push(id_free);

        }
        for(n=0;n<li_rest_len;n++){
            var id_rest=$(".js-sortable-rest li").eq(n).attr("id");

            arrRest.push(id_rest);

        }

        for(m=0;m<li_working_len;m++){
            var id_working=$(".js-sortable-working li").eq(m).attr("id");

            arrWorking.push(id_working);

        }

        localStorage.arrFree = arrFree;
        localStorage.arrWorking = arrWorking;
        localStorage.arrRest = arrRest;

        l_free=localStorage.arrFree;
        l_working=localStorage.arrWorking;
        l_rest=localStorage.arrRest;
        console.log("free:"+l_free);
        console.log("working:"+l_working);
        console.log("rest:"+l_rest);
    }

    //上下钟状态变更
    function change_states($this) {
        var select_val=$this.val();
        // console.log(select_val);
        if(select_val==="free"){
            $this.closest("li").removeClass("m-element-rest m-element-working sortable-drag").addClass("m-element-free")
                .appendTo(".js-sortable-free");
        }else if(select_val==="working"){
            $this.closest("li").removeClass("m-element-rest m-element-free sortable-drag").addClass("m-element-working")
                .appendTo(".js-sortable-working");
        }else if(select_val==="rest"){
            $this.closest("li").removeClass("m-element-working m-element-free sortable-drag").addClass("m-element-rest")
                .appendTo(".js-sortable-rest");
        }
        getWaiterStates();
        // console.log(`li_free_len:${li_free_len};li_working_len:${li_working_len};li_rest_len:${li_rest_len};localStorage:${localSt}`);

    }
    // $("li[id^='waiter'] select").each(function () {
    //     var $this=$(this);
    //     change_states($this);
    // });
    $("li[id^='waiter']").on('change','select',function () {
        var $this=$(this);
        // console.log(select_val);
        change_states($this);
    });
});